package com.cdvtc.fengchao.Servlet;

import com.cdvtc.fengchao.dao.GeneralDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/admin/productManagementServlet")
public class ProductManagementServlet extends HttpServlet {

    private GeneralDao generalDao = new GeneralDao();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 查询产品数据
        List<Map<String, Object>> products = generalDao.getProducts();
        System.out.println("查询到的产品数量: " + products.size());
        request.setAttribute("products", products);

        // 转发到JSP页面
        request.getRequestDispatcher("/admin/products.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");
        if ("add".equals(action)) {
            // 添加产品
            String photo = request.getParameter("photo");
            String name = request.getParameter("name");
            String category = request.getParameter("category");
            String price = request.getParameter("price");
            String features = request.getParameter("features");
            int supplierId = Integer.parseInt(request.getParameter("supplierId"));
            int stock = Integer.parseInt(request.getParameter("stock"));
            int result = generalDao.addProduct(photo, name, category, price, features, supplierId, stock);
            if (result > 0) {
                System.out.println("产品添加成功");
            } else {
                System.out.println("产品添加失败");
            }
        } else if ("update".equals(action)) {
            // 更新产品
            int id = Integer.parseInt(request.getParameter("id"));
            String photo = request.getParameter("photo");
            String name = request.getParameter("name");
            String category = request.getParameter("category");
            String price = request.getParameter("price");
            String features = request.getParameter("features");
            int supplierId = Integer.parseInt(request.getParameter("supplierId"));
            int stock = Integer.parseInt(request.getParameter("stock"));
            int result = generalDao.updateProduct(id, photo, name, category, price, features, supplierId, stock);
            if (result > 0) {
                System.out.println("产品更新成功");
            } else {
                System.out.println("产品更新失败");
            }
        } else if ("delete".equals(action)) {
            // 删除产品
            int id = Integer.parseInt(request.getParameter("id"));
            int result = generalDao.deleteProduct(id);
            if (result > 0) {
                System.out.println("产品删除成功");
            } else {
                System.out.println("产品删除失败");
            }
        }
        response.sendRedirect(request.getContextPath() + "/admin/productManagementServlet");
    }
}